#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <clutter/clutter.h>

ClutterContentGravity gravities[] = 
{
    CLUTTER_CONTENT_GRAVITY_TOP_LEFT,
    CLUTTER_CONTENT_GRAVITY_TOP,
    CLUTTER_CONTENT_GRAVITY_TOP_RIGHT,

    CLUTTER_CONTENT_GRAVITY_LEFT,
    CLUTTER_CONTENT_GRAVITY_CENTER,
    CLUTTER_CONTENT_GRAVITY_RIGHT,

    CLUTTER_CONTENT_GRAVITY_BOTTOM_LEFT,
    CLUTTER_CONTENT_GRAVITY_BOTTOM,
    CLUTTER_CONTENT_GRAVITY_BOTTOM_RIGHT,

    CLUTTER_CONTENT_GRAVITY_RESIZE_FILL,
    CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT,
};

static void on_tap(ClutterTapAction *action, ClutterActor *actor, ClutterText *label)
{
    (void)action;
    (void)label;
    static guint cur_gravity = 0;
    clutter_actor_save_easing_state(actor);
    clutter_actor_set_content_gravity(actor, gravities[cur_gravity]);
    clutter_actor_restore_easing_state(actor);

    if (cur_gravity++ >= G_N_ELEMENTS(gravities))
        cur_gravity = 0;
}

int main(int argc, char *argv[])
{
    ClutterActor *stage;
    ClutterContent *image;
    ClutterAction *action;
    GdkPixbuf *pixbuf;

    if (clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
        return EXIT_FAILURE;

    stage = clutter_stage_new();
    clutter_actor_set_name(stage, "Stage");
    clutter_stage_set_title(CLUTTER_STAGE(stage), "Content Box");
    clutter_stage_set_user_resizable(CLUTTER_STAGE(stage), TRUE);
    g_signal_connect(stage, "destroy", G_CALLBACK(clutter_main_quit), NULL);
    clutter_actor_show(stage);


    pixbuf = gdk_pixbuf_new_from_file("../assert/1.bmp", NULL);
    image = clutter_image_new();
    clutter_image_set_data(CLUTTER_IMAGE(image),
                           gdk_pixbuf_get_pixels(pixbuf),
                           gdk_pixbuf_get_has_alpha(pixbuf)
                               ? COGL_PIXEL_FORMAT_RGBA_8888
                               : COGL_PIXEL_FORMAT_RGB_888,
                           gdk_pixbuf_get_width(pixbuf),
                           gdk_pixbuf_get_height(pixbuf),
                           gdk_pixbuf_get_rowstride(pixbuf),
                           NULL);
    g_object_unref(pixbuf);

    clutter_actor_set_content_scaling_filters(stage,
        CLUTTER_SCALING_FILTER_TRILINEAR, CLUTTER_SCALING_FILTER_LINEAR);
    clutter_actor_set_content_gravity(stage, gravities[0]);
    clutter_actor_set_content(stage, image);
    g_object_unref(image);

    /* 创建一个action, 然后添加到stage,  也可以直接使用stage的button-press-event事件 */
    action = clutter_tap_action_new();
    g_signal_connect(action, "tap", G_CALLBACK(on_tap), NULL);
    clutter_actor_add_action(stage, action);

    clutter_main();
    return EXIT_SUCCESS;
}
